package com.szt.sy.service.system.rolebutton;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.szt.sy.entity.system.RoleButton;
import com.szt.sy.mapper.system.RoleButtonMapper;

/**
 * @author wwy
 *
 *         <p>
 *         说明：角色-按钮权限 服务<br/>
 *         创建时间：2018-03-14
 *         </p>
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class RoleButtonService extends ServiceImpl<RoleButtonMapper, RoleButton> {
	
	/**
	 * 列表
	 * 
	 * @param page
	 * @
	 */
	public Page<RoleButton> list(Page<RoleButton> page, Map<String, Object> map) {
		page.setRecords(baseMapper.dataList(page, map));
		return page;
	}
	
	/**
	 * 列表(全部)
	 * 
	 * @param pd
	 * @
	 */
	public List<RoleButton> listAll(Map<String, Object> map) {
		return baseMapper.dataList(map);
	}
	
	/****
	 * 修改角色按钮权限
	 * 
	 * @param roleId
	 *            角色id
	 * @param buttonId
	 *            按钮id
	 * @param menuIds
	 *            菜单id（数组）
	 */
	public void updateRoleButtons(String roleId, String buttonId, List<String> menuIds) {
		// 删除之前记录
		this.delete(new EntityWrapper<RoleButton>().eq("roleId", roleId).eq("buttonId", buttonId));
		if (menuIds == null || menuIds.isEmpty()) {
			return;
		}
		List<RoleButton> roleButtonList = new ArrayList<>(menuIds.size());
		for (String menuId : menuIds) {
			RoleButton roleButton = new RoleButton();
			roleButton.setButtonId(buttonId);
			roleButton.setMenuId(Long.parseLong(menuId));
			roleButton.setRoleId(roleId);
			roleButtonList.add(roleButton);
		}
		insertBatch(roleButtonList);
	}
	
	public List<RoleButton> selectMoreInfoByRoleId(String roleId) {
		Map<String, Object> queryParam = new HashMap<>(1);
		queryParam.put("roleId", roleId);
		return baseMapper.selectMoreInfo(queryParam);
	}
	
}
